package com.ihelp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ihelp.pojo.Article;
import com.ihelp.mapper.ArticleMapper;
import com.ihelp.pojo.Order;
import com.ihelp.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-06-03
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public List<Article> getAllArticle() {
        return articleMapper.selectList(null);
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleMapper.selectById(id);
    }

    @Override
    public List<Article> getArticleByDynamicConditions(Integer id, String fileName, Integer level, String time, String author) {
        QueryWrapper<Article> articleQueryWrapper = new QueryWrapper<>();

        for (int i = 0; i < 1; i++) {
            if (id != null && id > 0) {
                articleQueryWrapper.eq("id", id);
            }
            if (fileName != null) {
                articleQueryWrapper.eq("file_name", fileName);
            }
            if (level != null) {
                articleQueryWrapper.eq("level", level);
            }
            if (time != null) {
                articleQueryWrapper.eq("time", time);
            }
            if (author != null) {
                articleQueryWrapper.eq("author", author);
            }
        }

        List<Article> articleList = articleMapper.selectList(articleQueryWrapper);
        return articleList;
    }

    @Override
    public Integer createArticle(String fileName, Integer level, String time, String author) {
        Article article = new Article();
        article.setFileName(fileName);
        article.setLevel(level);
        article.setTime(time);
        article.setAuthor(author);
        articleMapper.insert(article);
        return null;
    }

}
